﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;

namespace HKEA.CMS.Core.BLL.Tbl
{
    public class tblUserBLL
    {
        private  ItblUser IDAL;
        private static object obj = new Object();
        private tblUserBLL()
        {
            IDAL = DataAccess.CreateItblUser();
        }

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        public static tblUserBLL GetInstance()
        {
            
            return new tblUserBLL();
        }

        /// <summary>
        /// 后台登录验证 返回 管理员 实体
        /// </summary>
        /// <param name="ent">管理员 实体</param>
        /// <param name="msg">返回的登录信息</param>
        /// <returns>管理员 实体</returns>
        public tblUser UserAuth(string account,string pwd,ref string msg)
        {
            tblUser entUser=new tblUser();
            if (!string.IsNullOrEmpty(account))
            {
                tblUser ent = new tblUser();
                ent.User_Name = account.Trim();
                entUser = GetENT(ent);
                if (entUser == null)
                {
                    ent.User_Name = string.Empty;
                    ent.User_Email = account.Trim();
                    entUser = GetENT(ent);
                }
            }
            else
                entUser = null;
            if (entUser != null)
            {
                if (entUser.User_Password.Trim() == pwd.Trim())
                {
                    msg = string.Empty;
                    return entUser;
                }
                else
                {
                    msg = "密码不匹配！";
                    return null;
                }
            }
            else
            {
                msg = "用户不存在！";
                return null;
            }
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public tblUser GetENT(tblUser ent)
        {
            if (ent != null)
            {
                try
                {
                    return IDAL.GetENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return null;
        }

        /// <summary>
        /// 插入一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int InsertENT(tblUser ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret= IDAL.InsertENT(ent);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int DeleteENT(tblUser ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.DeleteENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int UpdateENT(tblUser ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.UpdateENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<tblUser> GetListByENT(tblUser ent,PageParam param, string strPropertyName,string strOrderBy)
        {
            List<tblUser> list=new List<tblUser>();
            if (ent != null && param != null)
            {
                try
                {
                    list= IDAL.GetListByENT(ent,param,strPropertyName,strOrderBy);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，不带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public List<tblUser> GetListByENT(tblUser ent)
        {
            List<tblUser> list = new List<tblUser>();
            if (ent != null)
            {
                try
                {
                    list = IDAL.GetListByENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到符合条件的记录条数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int GetCountByENT(tblUser ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.GetCountByENT(ent);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 判断是否已存在符合条件的记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public bool IsExist(tblUser ent)
        {
            bool ret = false;
            if (ent != null)
            {
                if (GetCountByENT(ent) > 0)
                    ret = true;
            }
            return ret;
        }
    }
}
